package cn.kk.day02zuoye.controller;
/*
 *@author KK on 2022.11.22 19:02
 */

import cn.kk.day02zuoye.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action.equals("login")){
            String username = request.getParameter("username");
            if (username.equals("")&&username==null){
                request.setAttribute("msg","用户名不能为空");
            }else {
                User user = new User(username);
                request.getSession().setAttribute("user",user);
                request.setAttribute("msg","登陆成功");
            }
        }else if (action.equals("logout")){
            request.getSession().invalidate();
        }
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }
}
